package cn.stylefeng.guns.modular.cb.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cb.entity.CbCustomerStudents;
import cn.stylefeng.guns.modular.cb.mapper.CbCustomerStudentsMapper;
import cn.stylefeng.guns.modular.cb.model.params.CbCustomerStudentsParam;
import cn.stylefeng.guns.modular.cb.model.params.CbFocusCollectionParam;
import cn.stylefeng.guns.modular.cb.model.params.CbMessagesParam;
import cn.stylefeng.guns.modular.cb.model.result.CbCustomerStudentsResult;
import cn.stylefeng.guns.modular.cb.model.result.CbMessagesResult;
import cn.stylefeng.guns.modular.cb.model.result.CbObjectiveResult;
import cn.stylefeng.guns.modular.cb.service.*;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wj
 * @since 2021-01-07
 */
@Service
public class CbCustomerStudentsServiceImpl extends ServiceImpl<CbCustomerStudentsMapper, CbCustomerStudents> implements CbCustomerStudentsService {

    private static Logger logger = LoggerFactory.getLogger(CbCustomerStudentsServiceImpl.class);

    @Autowired
    private CbObjectiveService cbObjectiveService;

    @Autowired
    private CbFocusCollectionService cbFocusCollectionService;

    @Autowired
    private CbMessagesService cbMessagesService;

    @Autowired
    UserService userService;

    @Override
    public void add(CbCustomerStudentsParam param) {
        try {
            //添加学生用户角色
            LoginUser user = LoginContextHolder.getContext().getUser();
            this.userService.setRoles(user.getId(), String.valueOf(8));

            param.setPhone(user.getAccount());//用户电话=账户

            CbCustomerStudents entity = getEntity(param);
            entity.setUserStatus("ENABLE");
            entity.setContactSet(2);//手动获取联系方式
            this.save(entity);
            param.setId(entity.getId());
        } catch (Exception e) {
            logger.error("add error: ", e);
        }
    }

    @Override
    public void delete(CbCustomerStudentsParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(CbCustomerStudentsParam param) {
        try {
            CbCustomerStudents oldEntity = getOldEntity(param);
            CbCustomerStudents newEntity = getEntity(param);
            ToolUtil.copyProperties(newEntity, oldEntity);
            this.updateById(newEntity);
        } catch (Exception e) {
            logger.error("update error: ", e);
        }
    }

    @Override
    public CbCustomerStudentsResult findBySpec(CbCustomerStudentsParam param) {
        return null;
    }

    @Override
    public List<CbCustomerStudentsResult> findListBySpec(CbCustomerStudentsParam param) {
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(CbCustomerStudentsParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 获取学生信息
     *
     * @param param
     * @return
     */
    @Override
    public CbCustomerStudentsResult getStudent(CbCustomerStudentsParam param) {
        return this.baseMapper.getStudent(param);
    }

    private Serializable getKey(CbCustomerStudentsParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CbCustomerStudents getOldEntity(CbCustomerStudentsParam param) {
        return this.getById(getKey(param));
    }

    private CbCustomerStudents getEntity(CbCustomerStudentsParam param) {
        CbCustomerStudents entity = new CbCustomerStudents();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }


    /**
     * 学长的校友
     *
     * @param paramCondition
     * @return
     */
    @Override
    public LayuiPageInfo alumniByStudentList(CbCustomerStudentsParam paramCondition) {
        LoginUser user = LoginContextHolder.getContext().getUser();
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.alumniByStudentList(pageContext, paramCondition);
        try {
            List<CbCustomerStudentsResult> list = page.getRecords();
            for (CbCustomerStudentsResult student : list) {
                List<String> positionList = new ArrayList<>();
                List<CbObjectiveResult> objectiveList = cbObjectiveService.objectiveByLimit2(student.getCreateUser());
                for (CbObjectiveResult item : objectiveList) {
                    String positionName = item.getProfessionalName();
                    positionList.add(positionName);
                }
                student.setStringList(positionList);
                //是否关注
                CbFocusCollectionParam param = new CbFocusCollectionParam();
//                param.setUserId(paramCondition.getId());
                param.setCreateUser(user.getId());
                param.setObjectId(student.getId());
                param.setType(1);//关注
                Integer focus = cbFocusCollectionService.getFocusCollectionCount(param);
                student.setFocus(focus);

                //是否成功交换联系信息
                CbMessagesParam messagesParam = new CbMessagesParam();
                messagesParam.setCreateUser(user.getId());
                messagesParam.setMessageUserid(student.getId());
                messagesParam.setType(3L);//交互联系方式
//                messagesParam.setEventid(1L);//同意
//                Integer contact = cbMessagesService.getContactFlg(messagesParam);
                List<CbMessagesResult> jhList = cbMessagesService.getMessageByNow(messagesParam);
                Integer contact = 0;//是否请求
                Long eventId = 0L;//未处理
                if (jhList != null && jhList.size()>0) {
                    contact = jhList.size();
                    eventId = jhList.get(0).getEventid();//一天一条交换请求h
                }
                student.setContact(contact);
                student.setEventId(eventId);


                //是否已经邀请投递
                CbMessagesParam yqtd = new CbMessagesParam();
                yqtd.setType(6L);
                yqtd.setMessageUserid(student.getId());
                messagesParam.setCreateUser(user.getId());
                List<CbMessagesResult> yqList = cbMessagesService.getMessageByNow(yqtd);
                Integer yqCount = 0;
                if (yqList != null && yqList.size()>0) {
                    yqCount = yqList.size();
                }
                student.setYqCount(yqCount);
            }
            page.setRecords(list);
        } catch (Exception e) {
            logger.error("alumniByStudentList error: ", e);
        }
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 学生数量统计
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> queryDateCount(String startDate, String[] countArr) {
        return this.baseMapper.queryDateCount(startDate, countArr);
    }

    /**
     * 学生列表
     *
     * @param paramCondition
     * @return
     */
    @Override
    public LayuiPageInfo studentList(CbCustomerStudentsParam paramCondition) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.studentList(pageContext, paramCondition);
        return LayuiPageFactory.createPageInfo(page);
    }
}
